<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <title>通知公告表</title>
    <div th:replace="common/link::header"></div>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <form class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="noticeTitle" placeholder="请输入标题" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary" lay-submit lay-filter="reset">重置</button>
                </div>
            </div>
        </form>
        <div class="layui-card-body">
            <table id="noticeTable" lay-filter="noticeTable"></table>
            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <a sec:authorize="hasAuthority('notice:add')" class="layui-btn layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加</a>
                </div>
            </script>
            <script type="text/html" id="noticeStatusTpl">
                <input type="checkbox" lay-filter="noticeStatus" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" {{d.noticeStatus==='Y'?'checked':''}} />
            </script>
            <script type="text/html" id="contentTpl">
                <div class="layui-btn-container">
                    <a class="layui-btn layui-btn-xs" lay-event="info"><i class="layui-icon layui-icon-about"></i>查看</a>
                </div>
            </script>
            <script type="text/html" id="tableBar">
                <a sec:authorize="hasAuthority('notice:edit')" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a sec:authorize="hasAuthority('notice:del')" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<div th:replace="common/script::footer"></div>
<script th:inline="javascript">
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table','crud'], function() {
        let $ = layui.$,
            form = layui.form,
            table = layui.table,
            crud = layui.crud;
        //监听搜索
        form.on('submit(search)', function(data) {
            let field = data.field;
            //执行重载
            table.reload('noticeTable',{
                where: field
            });
        });
        // 修改通知公告状态
        form.on('switch(noticeStatus)', function (obj) {
            let noticeId = obj.elem.value;
            let checked = obj.elem.checked ? 'Y' : 'N';
            active.changeNoticeStatus(noticeId,checked);
        });
        //监听重置
        form.on('submit(reset)', function(data) {
            Object.keys(data.field).forEach(key => (data.field[key] = ''));
            table.reload('noticeTable',{
                where: data.field
            });
        });
        // 查询列表接口
        table.render({
            elem: '#noticeTable'
            ,toolbar: '#toolbar'
            ,height: 'full-110'
            ,url:  ctx + '/app/notice/page'
            ,cellMinWidth: 80
            ,page: true //开启分页
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', hide:true},
                {field: 'noticeTitle', title: '标题'},
                {field: 'noticeType', title: '公告类型',templet: function (d) { return crud.getDictValue('noticeType',d.noticeType);}},
                {field: 'noticeStatus', align: "center", templet: '#noticeStatusTpl', title: '公告状态'},
                {templet: '#contentTpl', title: '内容'},
                {toolbar: '#tableBar', title: '操作', width: 300, align:'center',fixed: 'right'}
            ]]
        });
        //头工具栏事件
        table.on('toolbar(noticeTable)', function(obj) {
            let checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data;
            switch(obj.event) {
                case 'add':
                    active.addView();
                    break;
            }
        });
        //监听行工具事件
        table.on('tool(noticeTable)', function(obj) {
            let data = obj.data //获得当前行数据
                ,layEvent = obj.event;
            if(layEvent === 'edit') {
                active.editView(data.id);
            } else if(layEvent === 'remove') {
                active.remove(data.id);
            } else if (layEvent === 'info') {
                active.showInfo(data);
            }
        });
        /* 触发弹层 */
        let active = {
            /**
             * 修改通知公告状态
             * @param noticeId 通知公告id
             * @param checked 状态
             */
            changeNoticeStatus: function (noticeId,checked) {
                $.ajax({
                    type: 'POST',
                    url:  ctx + '/app/notice/modify',
                    data: JSON.stringify({
                        id: noticeId,
                        noticeStatus: checked
                    }),
                    contentType:'application/json;charset=UTF-8',
                    dataType: 'json',
                    success: function(result) {
                        layer.msg(result.message);
                        if (result.code === 200) {
                            table.reload('userTable');
                        }
                    }
                });
            },
            /**
             * 添加
             * */
            addView: function() {
                layer.open({
                    type: 2
                    ,shade: 0.3
                    ,title: "添加通知公告表"
                    ,content: ctx + '/app/notice/addPage'
                    ,maxmin: true
                    ,scrollbar: false
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero) {
                        let submit = layero.find('iframe').contents().find('#save-submit');
                        submit.trigger('click');
                    }
                });
            },
            /**
             * 编辑
             * @param id id
             * */
            editView: function(id) {
                layer.open({
                    type: 2
                    ,shade: 0.3
                    ,title: '修改通知公告表'
                    ,content: ctx + '/app/notice/editPage/' + id
                    ,maxmin: true
                    ,scrollbar: false
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero) {
                        let submit = layero.find('iframe').contents().find('#save-submit');
                        submit.trigger('click');
                    }
                });
            },
            /**
             * 删除单个条目
             * @param id id
             */
            remove: function(id) {
                layer.confirm('真的删除行么', function(index) {
                    //向服务端发送删除指令
                    $.ajax({
                        type: 'DELETE',
                        url:  ctx + '/app/notice/remove/' + id,
                        contentType:'application/json;charset=UTF-8',
                        dataType: 'json',
                        success: function(result) {
                            layer.msg(result.message);
                            if (result.code === 200) {
                                layer.close(index);
                                table.reload('noticeTable');
                            }
                        }
                    });
                });
            },
            /**
             * 查看详情
             */
            showInfo(data) {
                layer.open({
                    type: 1
                    ,shade: 0.3
                    ,title: data.noticeTitle
                    ,content: data.noticeContent
                    ,maxmin: true
                    ,scrollbar: false
                    ,area: ['100%', '100%']
                    ,btn: ['关闭']
                    ,yes: function(index, layero) {
                        layer.close(index);
                    }
                });
            }
        }
    });
</script>
</body>
</html>
